﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       ToolIOMonitor
- 机器名称：       YTE
- 命名空间：       LD.Tool.BC
- 文 件 名：       ToolIOMonitor
- 创建时间：       2022/7/28 8:59:49
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using LD.BC;
using LD.Host;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;

namespace LD.Tool
{
    [Tool(FrmParam = "ToolIOGroupMonitorByUserFrm", Text = "IO_Group监控自定", ToolType = ToolEnum.BC)]
    class ToolIOGroupMonitorByUser : BaseTool
    {
        public override OpResult Execute(TB_Tool model)
        {
            stopwatch.Start();
            isSuccess = true;
            try
            {
                //Log(model, JsonConvert.SerializeObject(model.Param), LogLevel.一般);
                var param = Newtonsoft.Json.JsonConvert.DeserializeObject<ToolIOGroupMonitorByUserParam>(model.Param);
                if (param != null)
                {
                    bool flag = false;
                    //超时检测
                    if (param.IsTimeOutMonitor)
                    {
                        Stopwatch start = new Stopwatch();
                        start.Start();
                        while (start.ElapsedMilliseconds < param.TimeDate && !BCAlertHelper.GetErrorQty && !flag)
                        {
                            foreach (var item in param.IoMonitors)
                            {
                                int signal = -1;
                                if (item.IoType == (int)IoType.输入)
                                    signal = BcFactory.Instance.Control.GetIn(item.No);

                                if (item.IoType == (int)IoType.输出)
                                    signal = BcFactory.Instance.Control.GetOut(item.No);

                                if (signal == item.Low_Hig)
                                    flag = true;
                                else
                                    flag = false;

                                if (!flag)
                                    break;
                            }
                            Task.Delay(5).Wait();
                        }
                        start.Stop();
                    }
                    else
                    {
                        //无期限等待信号
                        while (!BCAlertHelper.GetErrorQty && !flag)
                        {
                            foreach (var item in param.IoMonitors)
                            {
                                int signal = -1;
                                if (item.IoType == (int)IoType.输入)
                                    signal = BcFactory.Instance.Control.GetIn(item.No);
                                if (item.IoType == (int)IoType.输出)
                                    signal = BcFactory.Instance.Control.GetOut(item.No);

                                if (signal == item.Low_Hig)
                                    flag = true;
                                else
                                    flag = false;

                                if (!flag)
                                    break;
                            }
                            Task.Delay(5).Wait();
                        }
                    }
                    isSuccess = flag;
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                message = ex.Message;
                Log(model, message, LogLevel.严重);
            }
            finally
            {
                stopwatch.Stop();
            }
            return new OpResult() { IsSuccess = isSuccess, Message = message, ExecuteTime = stopwatch.ElapsedMilliseconds };
        }
    }
}
